﻿@{
    ViewBag.Title = "通知设置";
}

@section scripts
{
    <link rel="stylesheet" href="../Scripts/kindeditor-4.1.10/themes/default/default.css" />
    <script charset="utf-8" src="../Scripts/kindeditor-4.1.10/kindeditor-min.js"></script>
    <script charset="utf-8" src="../Scripts/kindeditor-4.1.10/lang/zh_CN.js"></script>
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script>
}
<form id="form1" runat="server" method="post" style="height: 100%">
    <div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
        <table style="width: 100%;">
            <tr>
                <td style="width: 100%;">
                    <a class="mini-button" id="save" name="save" iconCls="icon-save" onclick="saveMessageSet" plain="true">保存</a>
                    <span class="separator"></span>
                    <a class="mini-button" iconCls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                </td>
            </tr>
        </table>
    </div>
    <div id = "DivHeader">
        <table>
            <tr>
                <td width="80px" height="30px" align="right">公众号(appid)：</td>
                <td>
                    <input id="FAppId" name="FAppId" class="mini-textbox" width="300" />
                </td>
            </tr> 
            <tr>
                <td width="80px" height="30px" align="right">公众号(Secret)：</td>
                <td>
                    <input id="FSecret" name="FSecret" class="mini-textbox" width="300"/>                    
                </td>
            </tr>   
            <tr>
                <td width="80px" height="30px"align="right">公众号(原始ID)：</td>
                <td>
                    <input id="ToUserName" name="ToUserName" class="mini-textbox" width="300"/>                    
                </td>
            </tr>    
            <tr>
                <td width="80px" height="30px"align="right">令牌(Token)：</td>
                <td>
                    <input id="FToken" name="FToken" readonly="readonly" class="mini-textbox" required="true" width="300"/>                    
                </td>
            </tr>  
            <tr>
                <td width="80px" height="30px"align="right">关联的公众号：</td>
                <td>
                    <input id="FParentMsgFlag" name="FParentMsgFlag" class="mini-combobox" style="width:300px;" showNullItem="false"
                     allowInput="false" align="left" value ="0" data ="dParentMsgFlag" onvaluechanged="datamsg_changed"/> 
                </td>
            </tr>                                      
        </table>     
        <table>
            <tr>
                <td width="130px" height="30px" align="right">会员服务到期提醒提前天数：</td>
                <td>
                    <input id="FMemberDays" name="FMemberDays" class="mini-spinner " maxlength="10" style="width:230px;" minValue="0"
                    maxValue="365" TabIndex="11" showbutton="false" allowNull="true" changeOnMousewheel="false" format="#" />天
                </td>
            </tr>   
            <tr>
                <td width="130px" height="30px" align="right">会员次卡到期提醒提前天数：</td>
                <td width="200px" >
                    <input id="FTimeDays" name="FTimeDays" class="mini-spinner " maxlength="10" style="width:230px;" minValue="0"
                    maxValue="365" TabIndex="11" showbutton="false" allowNull="true" changeOnMousewheel="false" format="#" />天
                </td>
            </tr>   
            <tr>
                <td width="130px" height="30px" align="right">沉睡会员超过多少天后提醒：</td>
                <td>
                    <input id="FMemberSleep" name="FMemberSleep" class="mini-spinner " maxlength="10" style="width:230px;" minValue="1"
                    maxValue="999999" TabIndex="11" showbutton="false" allowNull="true" changeOnMousewheel="false" format="#" />天
                </td>
                <td width="200px" height="30px" align="right">模板标题插入函数：</td>
                <td width="200px" >
                    <input class="mini-button" text="会员电话" style="width:80px" onclick="setMemberText('{电话号码}')" />  
                    <input class="mini-button" text="会员姓名" style="width:80px" onclick="setMemberText('{会员姓名}')" />  
                </td>
            </tr>                    
        </table>  
    </div>
    <div class="mini-fit">
        <div id="datagrid1" class="mini-datagrid" style="width:100%;height:100%;" 
            borderStyle="border:0;" showPager = "false" showPageSize="false" showPageIndex="false" 
            url="GetWxMsgAccount" allowCellSelect="true" allowCellEdit="true" showColumnsMenu="true" fitColumns="false">
            <div property="columns">
                <div type="checkboxcolumn" field="FIsEnabled" name="FIsEnabled" trueValue="1" falseValue="0" width="40" headerAlign="center">启用</div>
                <div field="FTemplateNumber" name="FTemplateNumber" width="130" headerAlign="center">模板编号</div>  
                <div field="FTemplateName" name="FTemplateName" width="140" headerAlign="center">模板标题</div>
                <div field="FMsgTemplateID" name="FMsgTemplateID" width="350" headerAlign="center">模板ID
                    <input property="editor" class="mini-textbox" style="width:100%;" />
                </div>
                <div field="FMsgTitle" name="FMsgTitle" width="300" headerAlign="center">消息标题
                    <input property="editor" class="mini-textbox" style="width:100%;" />
                </div>
                <div field="FMsgRemark" name="FMsgRemark" width="300" headerAlign="center">消息备注
                    <input property="editor" class="mini-textbox" style="width:100%;" />
                </div>
                <div field="FDataJson" name="FDataJson" width="300" headerAlign="center">详细内容</div>
            </div>  
        </div>
    </div>
</form>
<script>

    var dParentMsgFlag = [{ "id": "0", "text": "不关联" },
                        { "id": "1", "text": "关联总店公众号" },
                        { "id": "2", "text": "本店私有公众号" }]

    mini.parse();
    var form = new mini.Form("form1");
    var grid = mini.get("datagrid1");
    function FormShown() {
        mini.parse();
        getMessageSet();
    }

    function setMemberText(data) {        
        var row = grid.getSelected();
        if (row) {
            var sMsgTitle = row.FMsgTitle + data;
            grid.updateRow(row, { FMsgTitle: sMsgTitle });
        } else {
            alert("请选择一行数据后操作。");
        }
    }

    function saveMessageSet() {       
        var json_detail = mini.encode(grid.getData());
        var json_master = mini.encode(form.getData());
        var jsonstr = '{"json_detail":\'' + json_detail + '\',"json_master":\'' + json_master + '\'}';
        mini.mask({ el: document.body, cls: 'mini-mask-loading', html: '保存中, 请稍等......' });
        mini.get("save").setEnabled(false); 
        $.post("SaveMessageSet", { json: jsonstr, mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.unmask(document.body);
                message = mini.decode(ret.msg);
                getMessageSet();
                mini.showTips({ content: message[0].msg, state: "success", x: "center", y: "top", timeout: 3000 });
            }
            else {                
                mini.unmask(document.body);
                mini.alert("保存失败:" + ret.error);
            }
            mini.get("save").setEnabled(true); 
        });

    }

    function getMessageSet() {
        var param = {
            mSystemAppID: parent.window.getSystemAppID()
        }
        mini.get("save").setEnabled(true);
        $.ajax({
            url: "/MessageInform/GetMessageSet",
            data: param,
            type: "post",
            async: false,
            success: function (text) {
                var data = mini.decode(text);
                grid.setData(data.datadetail);
                if (data.datamaster.length > 0) {
                    var mdata = data.datamaster[0];
                    form.setData(mdata);
                    datamsg_changed();
                }
            }
        });
    }

    function datamsg_changed() {
        if (mini.get("FParentMsgFlag").getValue() == "2") {
            mini.getbyName("FAppId").setEnabled(true);
            mini.getbyName("FSecret").setEnabled(true);
            mini.getbyName("ToUserName").setEnabled(true);
            mini.getbyName("FAppId").setEnabled(true);
            grid.setAllowCellEdit(true);
        }
        else {
            mini.getbyName("FAppId").setEnabled(false);
            mini.getbyName("FSecret").setEnabled(false);
            mini.getbyName("ToUserName").setEnabled(false);
            mini.getbyName("FAppId").setEnabled(false);
            grid.setAllowCellEdit(false);
        }           
    }

</script>